﻿namespace PMS.Client.FrmUserPositionLevel
{
    using FastReport;
    using FastReport.Preview;
    using FastReport.Utils;
    using PMS.BLL;
    using PMS.Common;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;

    public class FrmReport : Form
    {
        private OrganReportBLL bll = new OrganReportBLL();
        private Button btnPrint;
        private CheckBox cbIncludeChild;
        private IContainer components = null;
        private Label label1;
        private OrganReport model = null;
        private int OrganID = 0;
        private Panel panel1;
        private PreviewControl previewControl1;
        private FastReport.Report report1 = null;
        private string reportPath = (Application.StartupPath + "//ReportTemplates//position.frx");

        public FrmReport(int OrganID)
        {
            this.OrganID = OrganID;
            this.InitializeComponent();
            this.model = this.bll.GetModel(this.OrganID);
            if (this.model != null)
            {
                this.report1 = new FastReport.Report();
                this.report1.Preview = this.previewControl1;
                this.report1.Load(this.reportPath);
                this.report1.RegisterData(this.GetAllChildData(), "OrganReport");
                try
                {
                    this.report1.Show();
                }
                catch (Exception exception)
                {
                    Log.Write(exception.Message);
                    MessageBox.Show("报表加载失败，请重试！");
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                this.report1.Print();
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                MessageBox.Show("未安装打印机或打印服务未开启！");
            }
        }

        private void cbIncludeChild_CheckedChanged(object sender, EventArgs e)
        {
            List<OrganReport> data = new List<OrganReport>();
            if (this.cbIncludeChild.Checked)
            {
                data = this.GetAllChildData();
            }
            else
            {
                this.model = this.bll.GetModel(this.OrganID);
                data.Add(this.model);
            }
            this.report1.RegisterData(data, "OrganReport");
            this.report1.Show();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private List<OrganReport> GetAllChildData()
        {
            List<OrganReport> list = new List<OrganReport>();
            List<OrganReport> source = this.bll.GetList(PMS.Common.Keys.GetAllOrganIDList(this.OrganID), " AND IsOffice=2 ");
            this.model.BDJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.BDJ_Count));
            this.model.BSY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.BSY_Count));
            this.model.DYY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.DYY_Count));
            this.model.FDYY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FDYY_Count));
            this.model.FGJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FGJ_Count));
            this.model.FKJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FKJ_Count));
            this.model.FTJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FTJ_Count));
            this.model.FXJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FXJ_Count));
            this.model.FXSY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FXSY_Count));
            this.model.FZRKY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.FZRKY_Count));
            this.model.IsDoublePosition_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.IsDoublePosition_Count));
            this.model.KY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.KY_Count));
            this.model.Leader_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.Leader_Count));
            this.model.M_10 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_10));
            this.model.M_3 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_3));
            this.model.M_4 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_4));
            this.model.M_5 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_5));
            this.model.M_6 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_6));
            this.model.M_7 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_7));
            this.model.M_8 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_8));
            this.model.M_9 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_9));
            this.model.M_BSY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_BSY_Count));
            this.model.M_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_Count));
            this.model.M_FKJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_FKJ_Count));
            this.model.M_FTJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_FTJ_Count));
            this.model.M_FXJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_FXJ_Count));
            this.model.M_KY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_KY_Count));
            this.model.M_ZKJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_ZKJ_Count));
            this.model.M_ZTJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_ZTJ_Count));
            this.model.M_ZXJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.M_ZXJ_Count));
            this.model.Manager_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.Manager_Count));
            this.model.S_1 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_1));
            this.model.S_10 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_10));
            this.model.S_11 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_11));
            this.model.S_12 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_12));
            this.model.S_13 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_13));
            this.model.S_2 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_2));
            this.model.S_3 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_3));
            this.model.S_4 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_4));
            this.model.S_5 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_5));
            this.model.S_6 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_6));
            this.model.S_7 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_7));
            this.model.S_8 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_8));
            this.model.S_9 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_9));
            this.model.S_CJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_CJ_Count));
            this.model.S_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_Count));
            this.model.S_FGJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_FGJ_Count));
            this.model.S_ZGJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_ZGJ_Count));
            this.model.S_ZJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.S_ZJ_Count));
            this.model.Skiller_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.Skiller_Count));
            this.model.UnLeader_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.UnLeader_Count));
            this.model.User_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.User_Count));
            this.model.UserBuild_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.UserBuild_Count));
            this.model.UserJobPostion_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.UserJobPostion_Count));
            this.model.W_0 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_0));
            this.model.W_1 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_1));
            this.model.W_2 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_2));
            this.model.W_3 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_3));
            this.model.W_4 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_4));
            this.model.W_5 = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_5));
            this.model.W_CJG_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_CJG_Count));
            this.model.W_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_Count));
            this.model.W_GJG_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_GJG_Count));
            this.model.W_GJJS_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_GJJS_Count));
            this.model.W_JS_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_JS_Count));
            this.model.W_PTG_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_PTG_Count));
            this.model.W_ZJG_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.W_ZJG_Count));
            this.model.Worker_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.Worker_Count));
            this.model.XSY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.XSY_Count));
            this.model.ZGJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.ZGJ_Count));
            this.model.ZKJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.ZKJ_Count));
            this.model.ZRKY_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.ZRKY_Count));
            this.model.ZTJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.ZTJ_Count));
            this.model.ZXJ_Count = source.Sum<OrganReport>((Func<OrganReport, int>) (f => f.ZXJ_Count));
            list.Add(this.model);
            return list;
        }

        private void InitializeComponent()
        {
            this.previewControl1 = new PreviewControl();
            this.panel1 = new Panel();
            this.cbIncludeChild = new CheckBox();
            this.btnPrint = new Button();
            this.label1 = new Label();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.previewControl1.BackColor = SystemColors.Control;
            this.previewControl1.Dock = DockStyle.Fill;
            this.previewControl1.Font = new Font("宋体", 9f);
            this.previewControl1.Location = new Point(0, 0);
            this.previewControl1.Margin = new Padding(0);
            this.previewControl1.Name = "previewControl1";
            this.previewControl1.PageOffset = new Point(0, 0);
            this.previewControl1.Size = new Size(0x3cc, 0x1ce);
            this.previewControl1.StatusbarVisible = false;
            this.previewControl1.TabIndex = 2;
            this.previewControl1.ToolbarVisible = false;
            this.previewControl1.UIStyle = UIStyle.VisualStudio2005;
            this.panel1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.panel1.Controls.Add(this.cbIncludeChild);
            this.panel1.Controls.Add(this.btnPrint);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = DockStyle.Bottom;
            this.panel1.Location = new Point(0, 0x1ce);
            this.panel1.Name = "panel1";
            this.panel1.Padding = new Padding(0, 3, 0, 3);
            this.panel1.Size = new Size(0x3cc, 30);
            this.panel1.TabIndex = 3;
            this.cbIncludeChild.AutoSize = true;
            this.cbIncludeChild.Checked = true;
            this.cbIncludeChild.CheckState = CheckState.Checked;
            this.cbIncludeChild.Location = new Point(4, 7);
            this.cbIncludeChild.Name = "cbIncludeChild";
            this.cbIncludeChild.Size = new Size(0x5e, 0x10);
            this.cbIncludeChild.TabIndex = 2;
            this.cbIncludeChild.Text = "包括下属机构";
            this.cbIncludeChild.UseVisualStyleBackColor = true;
            this.cbIncludeChild.CheckedChanged += new EventHandler(this.cbIncludeChild_CheckedChanged);
            this.btnPrint.BackColor = Color.White;
            this.btnPrint.Dock = DockStyle.Right;
            this.btnPrint.FlatStyle = FlatStyle.Flat;
            this.btnPrint.Location = new Point(880, 3);
            this.btnPrint.Margin = new Padding(0);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new Size(0x4b, 0x18);
            this.btnPrint.TabIndex = 0;
            this.btnPrint.Text = "打印";
            this.btnPrint.UseVisualStyleBackColor = false;
            this.btnPrint.Click += new EventHandler(this.btnPrint_Click);
            this.label1.Dock = DockStyle.Right;
            this.label1.Location = new Point(0x3bb, 3);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x11, 0x18);
            this.label1.TabIndex = 1;
            this.label1.Text = "  ";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x3cc, 0x1ec);
            base.Controls.Add(this.previewControl1);
            base.Controls.Add(this.panel1);
            base.Name = "FrmReport";
            this.Text = "FrmReport";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
        }
    }
}

